﻿<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
    version="1.0" xmlns:date="http://exslt.org/dates-and-times" xmlns:arf="http://blog.thekid.me.uk/arf"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="xsl x date arf">

	<xsl:param name="ItemsPerPage"/>
	<xsl:param name="qs-Page" />

	<xsl:template name="SelectCurrentItems">
		<xsl:param name="allItems"/>

		<xsl:variable name="endPos" select="number($qs-Page) * number($ItemsPerPage)"/>

		<xsl:choose>
			<!-- Is there a Query String for the page index -->
			<xsl:when test="$qs-Page">
				<xsl:apply-templates select="$allItems[position() &gt; number($endPos)-number($ItemsPerPage) and position() &lt; number($endPos)+1]"/>
			</xsl:when>

			<xsl:otherwise>
				<xsl:apply-templates select="$allItems[position() &lt; (number($ItemsPerPage)+1)]"/>
			</xsl:otherwise>
		</xsl:choose>


	</xsl:template>

	<xsl:template name="PrevNext">

		<xsl:param name="allItems"/>

		<xsl:variable name="endPos" select="number($qs-Page) * number($ItemsPerPage)"/>
		<xsl:variable name="rowCount" select="count($allItems)" />

		<xsl:variable name="pos">
			<xsl:choose>
				<xsl:when test="not($qs-Page)">1</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="number($qs-Page)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:if test="(number($endPos) &gt; number($ItemsPerPage)) or ((not($qs-Page) and $rowCount &gt; number($ItemsPerPage)) or (($endPos) and number($endPos) &lt; number($rowCount)))">

			<span>
				<xsl:if test="number($endPos) &gt; number($ItemsPerPage)">
					<!-- We are on another page...show the previous -->
					<a>
						<xsl:attribute name="href">
							<xsl:value-of select="arf:BuildQueryString(concat('Page=', string(number($pos)-1)), 'Page')"/>
						</xsl:attribute>
						&lt;&lt; Previous &#160;
					</a>
				</xsl:if>

				<xsl:if test="(not($qs-Page) and $rowCount &gt; $ItemsPerPage) or (($endPos) and number($endPos) &lt; number($rowCount))">
					<!-- There appear to be more posts...show a 'next' link -->
					<a>
						<xsl:attribute name="href">
							<xsl:value-of select="arf:BuildQueryString(concat('Page=', string(number($pos)+1)), 'Page')"/>
						</xsl:attribute>
						&#160;Next &gt;&gt;
					</a>
				</xsl:if>
			</span>

		</xsl:if>

	</xsl:template>

</xsl:stylesheet>
